﻿using System;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace QunProject.Cache
{
    /// <summary>
    /// HttpRuntime 缓存
    /// </summary>
    internal class HttpRuntimeCache : BaseCache, ICache
    {
        public void Clear(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }

        public void Clear()
        {
            var cache = HttpRuntime.Cache.GetEnumerator();
            while (cache.MoveNext())
            {
                HttpRuntime.Cache.Remove(cache.Key.ToString());
            }
        }

        public object Get(string key)
        {
            return string.IsNullOrWhiteSpace(key) ? null : HttpRuntime.Cache.Get(key);
        }

        public bool Set(string key, object value)
        {
            if (string.IsNullOrWhiteSpace(key) || value == null)
                return false;
            HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddMinutes(Seconds), TimeSpan.Zero, CacheItemPriority.Default, null);
            return Get(key) != null;
        }

        public bool Set(string key, object value, int seconds)
        {
            if (string.IsNullOrWhiteSpace(key) || value == null)
                return false;
            HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddMinutes(seconds), TimeSpan.Zero, CacheItemPriority.Default, null);
            return Get(key) != null;
        }
    }
}